package com.pxmeta.statistics.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pxmeta.converter.UvStatisticConverter;
import com.pxmeta.statistics.UvStatistic;
import com.pxmeta.statistics.bo.UvStatisticBO;
import com.pxmeta.statistics.bo.UvStatisticResult;
import com.pxmeta.statistics.mapper.UvStatisticMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shoushen.luan
 * @since 2022-10-20
 */
@Service
public class UvStatisticDAO {
    @Autowired
    private UvStatisticMapper uvStatisticMapper;
    @Autowired
    private UvStatisticConverter uvStatisticConverter;

    public int saveOrUpdate(UvStatisticBO statisticBO) {
        LambdaQueryWrapper<UvStatistic> wrapper = new LambdaQueryWrapper();
        wrapper.eq(UvStatistic::getServerHost, statisticBO.getServerHost());
        wrapper.eq(UvStatistic::getStatDate, statisticBO.getStatDate());
        UvStatistic uvStatistic = uvStatisticMapper.selectOne(wrapper);
        if (uvStatistic != null) {
            uvStatistic.setAccessCount(statisticBO.getAccessCount());
            return uvStatisticMapper.updateById(uvStatistic);
        } else {
            uvStatistic = uvStatisticConverter.convertFromUvStatisticBO(statisticBO);
            return uvStatisticMapper.insert(uvStatistic);
        }
    }

    public List<UvStatisticResult> getList() {
        return uvStatisticMapper.getStatList();
    }
}
